package com.springboot.blog.service.impl;

import com.springboot.blog.beans.Blog;
import com.springboot.blog.beans.PageBlog;
import com.springboot.blog.beans.User;
import com.springboot.blog.config.utils.R;
import com.springboot.blog.dao.BlogDao;
import com.springboot.blog.dao.UserDao;
import com.springboot.blog.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;
    @Resource
    private BlogDao blogDao;

    @Override
    public void checkUserLogin(String username, String password,Boolean rememberMe) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username,password,rememberMe);
        System.out.println("============"+token);
        subject.login(token);
    }

    @Override
    public User findUserAndPhone(String username,String phone){
        return userDao.findUserAndPhone(username,phone);
    }

    @Override
    public Boolean updateUser(String username, String password, String pwdsalt) {
        return userDao.updateUser(username,password,pwdsalt);
    }

    @Override
    public PageBlog<User> page(Integer pageNo, Integer pageSize) {
        PageBlog<User> page = new PageBlog<User>();
        //设置当前页面
        page.setPageNo(pageNo);

        //求当前页的开始索引
        int begin = (page.getPageNo()-1) * pageSize;
        //求当前页数据
        List<User> items = userDao.queryUserForItems(begin,pageSize);
        //设置当前页数据
        page.setItems(items);
        return page;
    }
}
